/**
 * @file connection.c
 * @brief Short description of this file.
 *
 * Complete description of this file.
 *
 * @date Apr 20, 2012
 * @author Francesco Corvino
 */
#include "connection.h"

void Connection_init(Connection *c, char *addr, char *port){
	/// - Alloco lo spazio necessario per address
	if((c->address = malloc(strlen(addr)+1))==NULL){
		c->address = addr;
	} else {
		strcpy(c->address, addr);
	}
	/// - Alloco lo spazio necessario per port
	if((c->port = malloc(strlen(port)+1))==NULL){
		c->port = port;
	} else {
		strcpy(c->port,port);
	}
	/// - Inizializzo i restanti valori
	c->sd = -1;
	c->recvbytes = 0;
	c->recvpkt = 0;
	c->sendbytes = 0;
	c->sendpkt =0;
	c->connectionId = -1;
	memset(&(c->dataProtocol),0,sizeof(HiddenType));
}

/**
 * @brief Elimina una connessione liberando la memoria occupata.
 *
 * @param c Connessione da eliminare.
 */
void Connection_destroy(Connection *c){
	free(c->address);
	free(c->port);
	HiddenType_destroy(&(c->dataProtocol));
	c->port = NULL;
	c->address = NULL;
	c->sd = -1;
}

void* Connection_getReport(Connection *c){
	return NULL;
}

int Connection_cmp(const void * a, const void *b){
	Connection *A = (Connection*)a;
	Connection *B = (Connection*)b;
	int r =0;
	if((r =strcmp(A->address,B->address))!=0) return r;
	if((r =strcmp(A->port,B->port))!=0) return r;
	if(A->protocolType>B->protocolType) return 1;
	if(A->protocolType<B->protocolType) return -1;
	return 0;
}
